// pages/food_mann/food_mann.js
const get_from = require('../../module/service.js').get_from;
const post_to = require('../../module/service.js').post_to;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    shop_id: 0,
    foods_list: [],
    menumsg: [],
    categories: [],
    all_categories: [],
    promotion: '',
    menumessage: {},
    menuindex: "a0", //索引的序号
    isleft: 0,
    isgoods: 0, //判断控制显示样式和满减的文字与价格
    count: 0,
    //这里将写组件相关的属性
    iscpn: false,
    translist: [], //单价:shopvalue，虚拟id:foods_id、总价:price、名字:name、数量:number
    tempdiscount: 0,
    is_onShow: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let shop_id = parseInt(options.shop_id);
    if (!shop_id) {
      wx.showToast({
        title: '非法商店号',
        icon: 'error',
      });
      return;
    }
    this.data.shop_id = shop_id;

    //获取所有的分类
    get_from("food/getAllCategories").then((res) => {
      this.data.all_categories = res.data;
    });

    //请求food/getFoodsByShopId
    this.getFoods(this.getCategories); //获取所有的分类
  },

  onShow() {
    console.log('abccc');
    //获取所有的分类
    get_from("food/getAllCategories").then((res) => {
      this.data.all_categories = res.data;
    });

    //请求food/getFoodsByShopId
    this.getFoods(this.getCategories); //获取所有的分类
  },

  getFoods(callback) {

    //请求后端获取商店菜单信息
    get_from('shop/getMenuByShopId', {
      shop_id: this.data.shop_id
    }).then((res) => {
      if (res.data.msg === 'error') {
        wx.showToast({
          title: '获取菜单失败',
          icon: 'error',
        });
        return;
      }

      this.data.foods_list = res.data;
      callback();
    });
  },

  getCategories() {
    //请求获取分类
    get_from("shop/getAllCategoriesByShopId", {
      shop_id: this.data.shop_id
    }).then((res) => {
      console.log(res.data);
      this.data.categories = res.data;
      let arr = [];
      for (let i = 0; i < this.data.categories.length; i++) {
        arr.push({
          name: this.data.categories[i],
          foods: []
        });
      }
      this.setData({
        menumsg: arr
      });
      console.log("set menumsg: ", this.data.menumsg);
      this.dealMenuMsg();
    }, () => {
      console.log("失败");
      wx.hideLoading();
    });
  },

  dealMenuMsg() {
    let tmp_menu = this.data.menumsg;
    // let all_categories = this.data.all_categories;
    let all_categories = [];
    get_from('shop/getAllCategoriesByShopId', {
      shop_id: this.data.shop_id
    }).then((res) => {
      all_categories = res.data;
    });

    //处理得到的数据
    let len = this.data.foods_list.length;
    for (let i = 0; i < tmp_menu.length; i++) {
      for (let j = 0; j < len; j++) {
        let tmp = this.data.foods_list[j];
        // console.log('tmp is:', tmp);
        for (let x = 0; x < tmp.categories.length; x++)
          if (tmp.categories[x] == tmp_menu[i].name) {
            tmp_menu[i].foods.push(tmp);
          }
      }
    }

    this.setData({
      menumsg: tmp_menu
    }, () => {
      // console.log(tmp_menu);
      // console.log(this.data.menumsg);
    });
  },

  //侧边菜单跳转
  orderJump(e) {
    this.setData({
      menuindex: 'a' + e.currentTarget.dataset.list,
      isleft: e.currentTarget.dataset.list
    })
  },

  food_detail(e) {
    let translist_id = 0;
    let cate_arr = this.data.categories;
    let food_id = 0;
    console.log(e);
    if (e.detail.foodid) {
      translist_id = e.detail.cpn_translist_id;
      food_id = e.detail.foodid;
    } else {
      food_id = e.currentTarget.dataset.foodid;
      translist_id = -1;
    }
    console.log(food_id);
    if (!food_id) {
      console.log("food_id is not defined!");
      return;
    }

    //对cate_arr进行编码
    let datalist = encodeURIComponent(JSON.stringify(cate_arr)); //函数把字符串编码为 URI 组件。有了编码自然也会有相对的解码函数

    //跳转
    wx.navigateTo({
      url: '/pages/fdetail_mann/fdetail_mann?food_id=' + food_id + '&cate_arr=' + datalist,
    });
  },

  cate_mann(){
    let food_list = [];
    let categories = this.data.categories;
    let len = this.data.foods_list.length;
    for (let i=0; i<len; i++)
    {
      food_list.push({
        name: this.data.foods_list[i].name, 
        checked: false, 
        categories: this.data.foods_list[i].categories
      });
    }
    let datalist = encodeURIComponent(JSON.stringify({
      food_list: food_list,
      categories: categories
    }));
    wx.navigateTo({
      url: '/pages/cate_mann/cate_mann?shop_id='+this.data.shop_id+'&datalist='+datalist
    });
  },

})